//-------------------------------------------------------------------------------
//	file CAudioCD.h
//	brief
//		CAudioCD header file
//
//	$Id: CAudioCD.h 240 2014-02-09 06:17:18Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2012 A.C. Verbeck
//	original author: Michel Helms (MichelHelms@Web.de).
//
//	This file is part of AudioCD: Audio CD function library
//
//	AudioCD is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	AudioCD is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with AudioCD.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
#pragma once

#include <windows.h>
#include <vector>

#include "CBuf.h"

//------------------------------------------------------------------------------
// CD-track structure
//------------------------------------------------------------------------------
struct CDTRACK {
	ULONG Address;
	ULONG Length;
};

//------------------------------------------------------------------------------
// CAudioCD class reads audio-tracks from CD.
// Basic functions only:
// - Constructor / Destructor
// - Open / Close / IsOpened
// - GetTrackCount / GetTrackTime / GetTrackSize
// - ReadTrack / ExtractTrack
// - LockCD / UnLockCD
// - LoadCD / EjectCD / IsCDReady
//------------------------------------------------------------------------------
class CAudioCD
{
public:
	// Constructor / Destructor
	CAudioCD(char Drive='\0');													// Initializes the class. If drive specified, the init calls "Open"
	~CAudioCD(void);															// CAudioCD destructor, acts like a call to "Close"

	// Open / Close / IsOpened
	bool Open(char Drive);														// Opens a handle to the drive, locks it and gets TOC
	void Close(void);															// Unlocks the CD-Drive and shuts down the access
	bool IsOpened(void);														// Returns whether the CD-drive has been opened

	// GetTrackCount / GetTrackTime / GetTrackSize
	ULONG GetTrackCount(void);													// Returns the number of tracks available. Returns 0xFFFFFFFF on failure (e.g. no "Open" called)
	ULONG GetTrackTime(ULONG Track);											// Returns length of audio-track (in seconds). Returns 0xFFFFFFFF on failure (e.g. no "Open" called)
	ULONG GetTrackSize(ULONG Track);											// Returns size of audio-track (in bytes). Returns 0xFFFFFFFF on failure (e.g. no "Open" called)

	// ReadTrack / ExtractTrack
	bool ReadTrack(ULONG Track, CBuf<char>* pBuf);								// Stores audio-track-data in a buffer. Buffer is allocated by ReadTrack. Note: A 4min audio-track is about 40MB
	bool ExtractTrack(ULONG Track, LPCTSTR Path);								// Saves a track to a file. File is in wav format (16-bit, 44.1kHz, stereo)

	// LockCD / UnLockCD
	bool LockCD(void);															// Locks CD-drive, user should not be able to open the drive
	bool UnlockCD(void);														// Unlocks the CD-drive, so it can be ejected

	// LoadCD / EjectCD / IsCDReady
	bool LoadCD(char drive);													// Loads CD
	bool EjectCD(void);															// Ejects CD
	bool IsCDReady(char Drive='\0');											// Returns true if CD is loaded and ready.	If no drive-letter specified, the CD must have been previously opened

protected:
	HANDLE					m_hCD;
	std::vector<CDTRACK>	m_aTracks;
};

//
//	End: CAudioCD.h
//
